package com.fc.test.shiro.service;


import com.fc.test.mapper.custom.PermissionDao;
import com.fc.test.mapper.custom.RoleDao;
import com.fc.test.model.auto.StuInfo;
import com.fc.test.model.auto.TsysPermission;
import com.fc.test.model.auto.TsysRole;
import com.fc.test.service.StuInfoService;
import com.fc.test.util.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentRealm extends AuthorizingRealm {
    @Autowired
    StuInfoService stuInfoService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        if(principals == null){
            throw new AuthorizationException("principals should not be null");
        }
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRole("stu");
        authorizationInfo.addStringPermission("stu");


        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        if (token.getPrincipal() == null) {
            return null;
        }
        UserToken userToken=(UserToken)token;
        StuInfo stuInfo=stuInfoService.findByAccount(userToken.getUsername());
        if (stuInfo==null){
            return  null;
        }else {
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                    stuInfo, // 用户对象
                    stuInfo.getPassword(), // 密码
                    getName() // realm name
            );
            return authenticationInfo;
        }

    }
}
